//+---------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation
//
// File: DsConfig.cs
//
// Contents: Create Performance Counters for Reputation Delivery Service
//
// Created by: Andrew Lin (yunlin) April 30 2008
//
//----------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Reflection;

namespace Microsoft.Reputation.Services
{
    static class Program
    {
        private static string Version = "V" + Assembly.GetExecutingAssembly().GetName().Version.Major.ToString();

        private static string DeliveryServiceEventLoggerSource = "Delivery Service " + Version;
        private static string DeliveryServiceEventLoggerName = "Reputation Services";

        private static string ReputationServicesPerfCounterCategory = "Reputation Services";
        private static string ReputationServicesPerfCounterCategoryHelp = "Microsoft Reputation Services Performance Counters.";

        private static string TotalRequestsCounterName = "DeliveryService." + Version + ".RequestsTotal";
        private static string TotalLookupRequestsCounterName = "DeliveryService." + Version + ".LookupRequestsTotal";
        private static string TotalListingRequestsCounterName = "DeliveryService." + Version + ".ListingRequestsTotal";
        private static string TotalGetOverridesRequestsCounterName = "DeliveryService." + Version + ".GetOverridesRequestsTotal";

        private static string TotalRequestsOutstandingCounterName = "DeliveryService." + Version + ".RequestsOutstanding";
        private static string TotalLookupRequestsOutstandingCounterName = "DeliveryService." + Version + ".LookupRequestsOutstanding";
        private static string TotalListingRequestsOutstandingCounterName = "DeliveryService." + Version + ".ListingRequestsOutstanding";
        private static string TotalGetOverridesRequestsOutstandingCounterName = "DeliveryService." + Version + ".GetOverridesRequestsOutstanding";

        private static string TotalRequestsThroughputCounterName = "DeliveryService." + Version + ".RequestsPerSec";
        private static string TotalLookupRequestsThroughputCounterName = "DeliveryService." + Version + ".LookupRequestsPerSec";
        private static string TotalListingRequestsThroughputCounterName = "DeliveryService." + Version + ".ListingRequestsPerSec";
        private static string TotalGetOverridesRequestsThroughputCounterName = "DeliveryService." + Version + ".GetOverridesRequestsPerSec";

        private static string TotalLookupCacheHitsCounterName = "DeliveryService." + Version + ".LookupCacheHitsTotal";
        private static string TotalLookupCacheMissesCounterName = "DeliveryService." + Version + ".LookupCacheMissesTotal";

        private static string LatencyDBLookupAverageCounterName = "DeliveryService." + Version + ".LatencyDBLookupAverage";
        private static string LatencyDBLookupBaseCounterName = "DeliveryService." + Version + ".LatencyDBLookupBase";

        private static string TelemetryServiceEventLoggerSource = "Telemetry Service";
        private static string TelemetryServiceEventLoggerName = "Reputation Services";

        private static string TotalSubmissionsCounterName = "TelemetryService.SubmissionsTotal";
        private static string TotalSubmissionsOutstandingCounterName = "TelemetryService.ActiveSubmissionsTotal";
        private static string TotalSubmissionsThroughputCounterName = "TelemetryService.SubmissionsPerSec";
        private static string TotalSubmissionDataThroughputCounterName = "TelemetryService.SubmmissionBytesPerSec";

        private static string FrontendCommonEventLoggerSource = "Frontend Common " + Version;
        private static string FrontendCommonEventLoggerName = "Frontend Common";

        //
        // Data Points Logger Perf Counters
        //
        private static string TSTotalLogFilesGeneratedCounterName = "TelemetryService.DataFilesGeneratedTotal";
        private static string TSTotalLogFilesGeneratedPerSecCounterName = "TelemetryService.DataFilesGeneratedPerSec";
        private static string TSTotalDataPointsEnqueuedCounterName = "TelemetryService.DataPointsEnqueuedTotal";
        private static string TSTotalDataPointsEnqueuedPerSecCounterName = "TelemetryService.DataPointsEnqueuedPerSec";
        private static string TSActiveQueueSizeCounterName = "TelemetryService.ActiveQueueSize";
        private static string TSProcessingQueueSizeCounterName = "TelemetryService.ProcessingQueueSize";
        private static string TSFailedFileQueueSizeCounterName = "TelemetryService.FailedFileQueueSize";
        private static string TSDataPersistThroughputCounterName = "TelemetryService.DataBytesPersistedPerSec";

        private static string DSTotalLogFilesGeneratedCounterName = "DeliveryService." + Version + ".DataFilesGeneratedTotal";
        private static string DSTotalLogFilesGeneratedPerSecCounterName = "DeliveryService." + Version + ".DataFilesGeneratedPerSec";
        private static string DSTotalDataPointsEnqueuedCounterName = "DeliveryService." + Version + ".DataPointsEnqueuedTotal";
        private static string DSTotalDataPointsEnqueuedPerSecCounterName = "DeliveryService." + Version + ".DataPointsEnqueuedPerSec";
        private static string DSActiveQueueSizeCounterName = "DeliveryService." + Version + ".ActiveQueueSize";
        private static string DSProcessingQueueSizeCounterName = "DeliveryService." + Version + ".ProcessingQueueSize";
        private static string DSFailedFileQueueSizeCounterName = "DeliveryService." + Version + ".FailedFileQueueSize";
        private static string DSDataPersistThroughputCounterName = "DeliveryService." + Version + ".DataBytesPersistedPerSec";


        private static void CreateReputationServicesPerfCounters()
        {
            CounterCreationDataCollection counterCollection = new CounterCreationDataCollection();

            // Create the Delivery Service total requests counter.
            CounterCreationData totalRequests = new CounterCreationData();
            totalRequests.CounterType = PerformanceCounterType.NumberOfItems64;
            totalRequests.CounterName = TotalRequestsCounterName;
            counterCollection.Add(totalRequests);

            // Create the Delivery Service total lookup requests counter.
            CounterCreationData totalLookupRequests = new CounterCreationData();
            totalLookupRequests.CounterType = PerformanceCounterType.NumberOfItems64;
            totalLookupRequests.CounterName = TotalLookupRequestsCounterName;
            counterCollection.Add(totalLookupRequests);

            // Create the Delivery Service total listing requests counter.
            CounterCreationData totalListingRequests = new CounterCreationData();
            totalListingRequests.CounterType = PerformanceCounterType.NumberOfItems64;
            totalListingRequests.CounterName = TotalListingRequestsCounterName;
            counterCollection.Add(totalListingRequests);

            // Create the Delivery Service total GetOverrides requests counter.
            CounterCreationData getOverridesRequests = new CounterCreationData();
            getOverridesRequests.CounterType = PerformanceCounterType.NumberOfItems64;
            getOverridesRequests.CounterName = TotalGetOverridesRequestsCounterName;
            counterCollection.Add(getOverridesRequests);

            // Create the Delivery Service total requests outstanding counter.
            CounterCreationData totalRequestsOutstanding = new CounterCreationData();
            totalRequestsOutstanding.CounterType = PerformanceCounterType.NumberOfItems64;
            totalRequestsOutstanding.CounterName = TotalRequestsOutstandingCounterName;
            counterCollection.Add(totalRequestsOutstanding);

            // Create the Delivery Service total lookup requests outstanding counter.
            CounterCreationData totalLookupRequestsOutstanding = new CounterCreationData();
            totalLookupRequestsOutstanding.CounterType = PerformanceCounterType.NumberOfItems64;
            totalLookupRequestsOutstanding.CounterName = TotalLookupRequestsOutstandingCounterName;
            counterCollection.Add(totalLookupRequestsOutstanding);

            // Create the Delivery Service total listing requests outstanding counter.
            CounterCreationData totalListingRequestsOutstanding = new CounterCreationData();
            totalListingRequestsOutstanding.CounterType = PerformanceCounterType.NumberOfItems64;
            totalListingRequestsOutstanding.CounterName = TotalListingRequestsOutstandingCounterName;
            counterCollection.Add(totalListingRequestsOutstanding);

            // Create the Delivery Service total requests throughput counter.
            CounterCreationData totalRequestsThroughput = new CounterCreationData();
            totalRequestsThroughput.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            totalRequestsThroughput.CounterName = TotalRequestsThroughputCounterName;
            counterCollection.Add(totalRequestsThroughput);

            // Create the Delivery Service total lookup requests throughput counter.
            CounterCreationData totalLookupRequestsThroughput = new CounterCreationData();
            totalLookupRequestsThroughput.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            totalLookupRequestsThroughput.CounterName = TotalLookupRequestsThroughputCounterName;
            counterCollection.Add(totalLookupRequestsThroughput);

            // Create the Delivery Service total listing requests throughput counter.
            CounterCreationData totalListingRequestsThroughput = new CounterCreationData();
            totalListingRequestsThroughput.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            totalListingRequestsThroughput.CounterName = TotalListingRequestsThroughputCounterName;
            counterCollection.Add(totalListingRequestsThroughput);

            // Create the Delivery Service Total GetOverrides Requests Outstanding counter.
            CounterCreationData totalGetOverridesOutstanding = new CounterCreationData();
            totalGetOverridesOutstanding.CounterType = PerformanceCounterType.NumberOfItems64;
            totalGetOverridesOutstanding.CounterName = TotalGetOverridesRequestsOutstandingCounterName;
            counterCollection.Add(totalGetOverridesOutstanding);

            // Create the Delivery Service Total GetOverrides Requests Throughput counter.
            CounterCreationData totalGetOverridesThroughput = new CounterCreationData();
            totalGetOverridesThroughput.CounterType = PerformanceCounterType.NumberOfItems64;
            totalGetOverridesThroughput.CounterName = TotalGetOverridesRequestsThroughputCounterName;
            counterCollection.Add(totalGetOverridesThroughput);

            // Create the Delivery Service total lookup application cache hits counter.
            CounterCreationData totalLookupApplicationCacheHits = new CounterCreationData();
            totalLookupApplicationCacheHits.CounterType = PerformanceCounterType.NumberOfItems64;
            totalLookupApplicationCacheHits.CounterName = TotalLookupCacheHitsCounterName;
            counterCollection.Add(totalLookupApplicationCacheHits);

            // Create the Delivery Service total lookup application cache misses counter.
            CounterCreationData totalLookupApplicationCacheMisses = new CounterCreationData();
            totalLookupApplicationCacheMisses.CounterType = PerformanceCounterType.NumberOfItems64;
            totalLookupApplicationCacheMisses.CounterName = TotalLookupCacheMissesCounterName;
            counterCollection.Add(totalLookupApplicationCacheMisses);


            // Create the Delivery Service average database latency lookup counter.
            CounterCreationData latencyDBLookupAverage = new CounterCreationData();
            latencyDBLookupAverage.CounterType = PerformanceCounterType.AverageTimer32;
            latencyDBLookupAverage.CounterName = LatencyDBLookupAverageCounterName;
            counterCollection.Add(latencyDBLookupAverage);


            // Create the Delivery Service average database latency lookup counter base.
            CounterCreationData latencyDBLookupBase = new CounterCreationData();
            latencyDBLookupBase.CounterType = PerformanceCounterType.AverageBase;
            latencyDBLookupBase.CounterName = LatencyDBLookupBaseCounterName;
            counterCollection.Add(latencyDBLookupBase);
            
            //
            // Create Data Points Logger Performance Counters in Delivery Services
            //
            CounterCreationData dsTotalLogFilesGenerated = new CounterCreationData();
            dsTotalLogFilesGenerated.CounterType = PerformanceCounterType.NumberOfItems64;
            dsTotalLogFilesGenerated.CounterName = DSTotalLogFilesGeneratedCounterName;
            counterCollection.Add(dsTotalLogFilesGenerated);

            CounterCreationData dsTotalLogFilesGeneratedPerSec = new CounterCreationData();
            dsTotalLogFilesGeneratedPerSec.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            dsTotalLogFilesGeneratedPerSec.CounterName = DSTotalLogFilesGeneratedPerSecCounterName;
            counterCollection.Add(dsTotalLogFilesGeneratedPerSec);

            CounterCreationData dsTotalDataPointsEnqueued = new CounterCreationData();
            dsTotalDataPointsEnqueued.CounterType = PerformanceCounterType.NumberOfItems64;
            dsTotalDataPointsEnqueued.CounterName = DSTotalDataPointsEnqueuedCounterName;
            counterCollection.Add(dsTotalDataPointsEnqueued);

            CounterCreationData dsTotalDataPointsEnqueuedPerSec = new CounterCreationData();
            dsTotalDataPointsEnqueuedPerSec.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            dsTotalDataPointsEnqueuedPerSec.CounterName = DSTotalDataPointsEnqueuedPerSecCounterName;
            counterCollection.Add(dsTotalDataPointsEnqueuedPerSec);

            CounterCreationData dsActiveQueueSize = new CounterCreationData();
            dsActiveQueueSize.CounterType = PerformanceCounterType.NumberOfItems64;
            dsActiveQueueSize.CounterName = DSActiveQueueSizeCounterName;
            counterCollection.Add(dsActiveQueueSize);

            CounterCreationData dsProcessingQueueSize = new CounterCreationData();
            dsProcessingQueueSize.CounterType = PerformanceCounterType.NumberOfItems64;
            dsProcessingQueueSize.CounterName = DSProcessingQueueSizeCounterName;
            counterCollection.Add(dsProcessingQueueSize);

            CounterCreationData dsFailedFileQueueSize = new CounterCreationData();
            dsFailedFileQueueSize.CounterType = PerformanceCounterType.NumberOfItems64;
            dsFailedFileQueueSize.CounterName = DSFailedFileQueueSizeCounterName;
            counterCollection.Add(dsFailedFileQueueSize);

            CounterCreationData dsDataPersistThroughput = new CounterCreationData();
            dsDataPersistThroughput.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            dsDataPersistThroughput.CounterName = DSDataPersistThroughputCounterName;
            counterCollection.Add(dsDataPersistThroughput);

            // Create the Telemetry Service total Submissions counter.
            CounterCreationData totalSubmissions = new CounterCreationData();
            totalSubmissions.CounterType = PerformanceCounterType.NumberOfItems64;
            totalSubmissions.CounterName = TotalSubmissionsCounterName;
            counterCollection.Add(totalSubmissions);

            // Create the Telemetry Service total Submissions outstanding counter.
            CounterCreationData totalSubmissionsOutstanding = new CounterCreationData();
            totalSubmissionsOutstanding.CounterType = PerformanceCounterType.NumberOfItems64;
            totalSubmissionsOutstanding.CounterName = TotalSubmissionsOutstandingCounterName;
            counterCollection.Add(totalSubmissionsOutstanding);

            // Create the Telemetry Service total Submissions throughput counter.
            CounterCreationData totalSubmissionsThroughput = new CounterCreationData();
            totalSubmissionsThroughput.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            totalSubmissionsThroughput.CounterName = TotalSubmissionsThroughputCounterName;
            counterCollection.Add(totalSubmissionsThroughput);

            // Create the Telemetry Service total submission data throughput counter.
            CounterCreationData totalSubmissionDataThroughput = new CounterCreationData();
            totalSubmissionDataThroughput.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            totalSubmissionDataThroughput.CounterName = TotalSubmissionDataThroughputCounterName;
            counterCollection.Add(totalSubmissionDataThroughput);

            //
            // Create Data Points Logger Performance Counters in Telemetry Services
            //
            CounterCreationData tsTotalLogFilesGenerated = new CounterCreationData();
            tsTotalLogFilesGenerated.CounterType = PerformanceCounterType.NumberOfItems64;
            tsTotalLogFilesGenerated.CounterName = TSTotalLogFilesGeneratedCounterName;
            counterCollection.Add(tsTotalLogFilesGenerated);

            CounterCreationData tsTotalLogFilesGeneratedPerSec = new CounterCreationData();
            tsTotalLogFilesGeneratedPerSec.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            tsTotalLogFilesGeneratedPerSec.CounterName = TSTotalLogFilesGeneratedPerSecCounterName;
            counterCollection.Add(tsTotalLogFilesGeneratedPerSec);

            CounterCreationData tsTotalDataPointsEnqueued = new CounterCreationData();
            tsTotalDataPointsEnqueued.CounterType = PerformanceCounterType.NumberOfItems64;
            tsTotalDataPointsEnqueued.CounterName = TSTotalDataPointsEnqueuedCounterName;
            counterCollection.Add(tsTotalDataPointsEnqueued);

            CounterCreationData tsTotalDataPointsEnqueuedPerSec = new CounterCreationData();
            tsTotalDataPointsEnqueuedPerSec.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            tsTotalDataPointsEnqueuedPerSec.CounterName = TSTotalDataPointsEnqueuedPerSecCounterName;
            counterCollection.Add(tsTotalDataPointsEnqueuedPerSec);

            CounterCreationData tsActiveQueueSize = new CounterCreationData();
            tsActiveQueueSize.CounterType = PerformanceCounterType.NumberOfItems64;
            tsActiveQueueSize.CounterName = TSActiveQueueSizeCounterName;
            counterCollection.Add(tsActiveQueueSize);

            CounterCreationData tsProcessingQueueSize = new CounterCreationData();
            tsProcessingQueueSize.CounterType = PerformanceCounterType.NumberOfItems64;
            tsProcessingQueueSize.CounterName = TSProcessingQueueSizeCounterName;
            counterCollection.Add(tsProcessingQueueSize);

            CounterCreationData tsFailedFileQueueSize = new CounterCreationData();
            tsFailedFileQueueSize.CounterType = PerformanceCounterType.NumberOfItems64;
            tsFailedFileQueueSize.CounterName = TSFailedFileQueueSizeCounterName;
            counterCollection.Add(tsFailedFileQueueSize);

            CounterCreationData tsDataPersistThroughput = new CounterCreationData();
            tsDataPersistThroughput.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
            tsDataPersistThroughput.CounterName = TSDataPersistThroughputCounterName;
            counterCollection.Add(tsDataPersistThroughput);

            // Create the category.
            PerformanceCounterCategory.Create(
                ReputationServicesPerfCounterCategory,
                ReputationServicesPerfCounterCategoryHelp,
                PerformanceCounterCategoryType.SingleInstance,
                counterCollection);
        }

        static int Main(string[] args)
        {
            try
            {
                if (PerformanceCounterCategory.Exists(ReputationServicesPerfCounterCategory))
                {
                    PerformanceCounterCategory.Delete(ReputationServicesPerfCounterCategory);
                }

                CreateReputationServicesPerfCounters();
                Console.WriteLine("Reputation Services Perf Counters created.");

                if (!EventLog.SourceExists(DeliveryServiceEventLoggerSource))
                {
                    EventLog.CreateEventSource(DeliveryServiceEventLoggerSource, DeliveryServiceEventLoggerName);
                    Console.WriteLine("Delivery Service Event Logger created.");
                }
                else
                {
                    Console.WriteLine("Delivery Service Event Logger already exist.");
                }

                if (!EventLog.SourceExists(TelemetryServiceEventLoggerSource))
                {
                    EventLog.CreateEventSource(TelemetryServiceEventLoggerSource, TelemetryServiceEventLoggerName);
                    Console.WriteLine("Telemetry Service Event Logger created.");
                }
                else
                {
                    Console.WriteLine("Telemetry Service Event Logger already exist.");
                }

                if (!EventLog.SourceExists(FrontendCommonEventLoggerSource))
                {
                    EventLog.CreateEventSource(FrontendCommonEventLoggerSource, FrontendCommonEventLoggerName);
                    Console.WriteLine("Frontend Common Event Logger created.");
                }
                else
                {
                    Console.WriteLine("Frontend Common Event Logger already exist.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to create Delivery/Telemetry Service Perf Counters or Event Logger:");
                Console.WriteLine(ex);
                return 1;
            }

            return 0;
        }
    }
}
